using System;
using System.Threading.Tasks;
using Admin2025.Domain.Entities.App;
using Admin2025.Domain.Repositories;

namespace Admin2025.Application
{
    public class UserService : IUserService
    {
        private readonly IRepository<AppUser> _userRepository;
        private readonly IRepository<AppUserRole> _userRoleRepository;

        public UserService(IRepository<AppUser> userRepository, IRepository<AppUserRole> userRoleRepository)
        {
            _userRepository = userRepository;
            _userRoleRepository = userRoleRepository;
        }

        public async Task<bool> AuthorizeAsync(string userName, string password)
        {
            // 简单示例，实际应加密比对
            var user = await _userRepository.FirstOrDefaultAsync(u => u.UserName == userName && u.Password == password);
            return user != null;
        }

        public async Task AssignRoleAsync(Guid userId, Guid roleId)
        {
            var userRole = new AppUserRole
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                RoleId = roleId,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                IsActived = true,
                IsDeleted = false,
                CreatedBy = Guid.Empty,
                UpdatedBy = Guid.Empty,
                DisplayOrder = 0,
                Remarks = string.Empty
            };
            await _userRoleRepository.InsertAsync(userRole);
        }
    }
}
